package com.liusand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.stream.CollectorUtil;
import cn.hutool.core.util.IdUtil;
import com.liusand.common.ThreadLocalUtils;
import com.liusand.dto.SetmealDishesDto;
import com.liusand.dto.SetmealDto;
import com.liusand.exception.SetmealException;
import com.liusand.mapper.SetmealMapper;
import com.liusand.pojo.PageInfo;
import com.liusand.pojo.Setmeal;
import com.liusand.pojo.SetmealDish;
import com.liusand.service.SetmealService;
import com.liusand.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author :liusha
 * packageName :com.liusand.service.impl
 * fileName :SetmealServiceImpl
 * creationTime :2022/11/27 10:13
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveSetmeal(SetmealDto setmealDto) {
        if(setmealDto == null){
            return false;
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmeal.setId(IdUtil.getSnowflakeNextIdStr());
        String id = ThreadLocalUtils.getId();
        setmeal.setCreateUser(id);
        setmeal.setUpdateUser(id);
        int setmealResult = setmealMapper.saveSetmeal(setmeal);

        List<SetmealDishesDto> setmealDishesDtoList = setmealDto.getSetmealDishes();
        if(setmealDishesDtoList == null){
            return true;
        }
        List<SetmealDish> setmealDishList = new ArrayList<>();
        for(SetmealDishesDto dto : setmealDishesDtoList){
            if(dto == null){
                continue;
            }
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setId(IdUtil.getSnowflakeNextIdStr());
            setmealDish.setDishId(dto.getDishId());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setUpdateUser(id);
            setmealDish.setCreateUser(id);
            setmealDishList.add(setmealDish);
        }
        int setmealDishResult = setmealMapper.saveSetmealDish(setmealDishList);
        if(setmealResult != 1 && setmealDishResult != setmealDishList.size()){
            throw new SetmealException("新增失败");
        }
        return true;

    }

    @Override
    public PageInfo<SetmealVo> page(String name, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<SetmealVo> setmeals = setmealMapper.page(name, start, pageSize);
        if(setmeals == null){
            return null;
        }
        PageInfo<SetmealVo> info = new PageInfo<>();
        info.setSize(pageSize);
        info.setCurrent(page);
        Long total = setmealMapper.countSetmeal(name);
        info.setTotal(total);
        info.setRecords(setmeals);
        return info;
    }

    @Override
    public SetmealAndDishVo findSetmealById(String id) {
        if(!StringUtils.hasText(id)){
            throw new SetmealException("查询失败");
        }
        SetmealAndDishVo setmealAndDishVo = setmealMapper.findSetmealById(id);
        if(setmealAndDishVo == null){
            throw new SetmealException("查询为空");
        }
        List<SetmealDishVo> setmealDishVo = setmealMapper.findSetmealDishById(id);
        setmealAndDishVo.setSetmealDishes(setmealDishVo);
        return setmealAndDishVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSetmeal(List<String> ids) {
        if(ids == null){
            return false;
        }
        int rightSetmealDishResult = setmealMapper.countSetmealDishByIds(ids);
        int setmealDishResult = setmealMapper.deleteSetmealDishByIds(ids);
        if(setmealDishResult != rightSetmealDishResult){
            throw new SetmealException("删除失败");
        }
        int setmealResult = setmealMapper.deleteSetmealByIds(ids);
        if(setmealResult != ids.size()){
            throw new SetmealException("删除失败");
        }
        return true;
    }

    @Override
    public boolean updateSetmeal(SetmealDto setmealDto) {
        if(setmealDto == null){
            return false;
        }
        String id = ThreadLocalUtils.getId();

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);

        setmeal.setUpdateUser(id);
        setmeal.setUpdateTime(LocalDateTime.now());
        Integer setmealResult = setmealMapper.updateSetmeal(setmeal);
        if(setmealResult != 1){
            throw new SetmealException("更新失败");
        }
        List<String> ids = new ArrayList<>();
        ids.add(setmealDto.getId());
        int rightSetmealDishResult = setmealMapper.countSetmealDishByIds(ids);
        int setmealDishResult = setmealMapper.deleteSetmealDishByIds(ids);
        if(setmealDishResult != rightSetmealDishResult){
            throw new SetmealException("更新失败");
        }
        List<SetmealDishesDto> setmealDishesDtoList = setmealDto.getSetmealDishes();
        if(setmealDishesDtoList == null){
            return true;
        }
        List<SetmealDish> setmealDishList = new ArrayList<>();
        for(SetmealDishesDto dto : setmealDishesDtoList){
            if(dto == null){
                continue;
            }
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setId(IdUtil.getSnowflakeNextIdStr());
            setmealDish.setDishId(dto.getDishId());
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setUpdateUser(id);
            setmealDish.setCreateUser(id);
            setmealDishList.add(setmealDish);
        }
        setmealDishResult = setmealMapper.saveSetmealDish(setmealDishList);
        if(setmealDishResult != setmealDishesDtoList.size()){
            throw new SetmealException("更新失败");
        }
        return true;
    }

    @Override
    public boolean updateStatus(List<String> ids, Integer status) {
        if(ids == null || status == null){
            return false;
        }
        Integer statusResult = setmealMapper.updateStatus(ids, status);
        if(statusResult != ids.size()){
            return false;
        }
        return true;
    }

    @Override
    public List<ClientSetmealVo> list(String categoryId, Integer status) {
        List<ClientSetmealVo> clientSetmealVoList = setmealMapper.list(categoryId, status);
        return clientSetmealVoList;
    }

    @Override
    public ClientSetmealVo findSetmealByIdClient(String categoryId) {
        if(categoryId == null){
            throw new SetmealException("查询套餐信息失败");
        }
        SetmealAndDishVo setmeals = setmealMapper.findSetmealById(categoryId);
        if(setmeals == null){
            throw new SetmealException("查询套餐信息失败");
        }
        ClientSetmealVo vo = new ClientSetmealVo();
        BeanUtils.copyProperties(setmeals, vo);
        Set<String> ids = new HashSet<>();
        ids.add(vo.getId());
        List<ClientSetmealDishVo> clientSetmealDishVoList = setmealMapper.findSetmealDishBySetmealIds(ids);
        if(clientSetmealDishVoList != null){
            Map<String, List<ClientSetmealDishVo>> map = clientSetmealDishVoList.stream().collect(CollectorUtil.groupingBy(ClientSetmealDishVo::getSetmealId));
            vo.setSetmealDishes(map.get(vo.getId()));
        }
        return vo;
    }
}
